﻿
namespace WebRSSBuilder.Web
{
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.ComponentModel.DataAnnotations;
    using System.Data;
    using System.Linq;
    using System.ServiceModel.DomainServices.EntityFramework;
    using System.ServiceModel.DomainServices.Hosting;
    using System.ServiceModel.DomainServices.Server;


    // Implements application logic using the ASPNETDBEntities context.
    // TODO: Add your application logic to these methods or in additional methods.
    // TODO: Wire up authentication (Windows/ASP.NET Forms) and uncomment the following to disable anonymous access
    // Also consider adding roles to restrict access as appropriate.
    // [RequiresAuthentication]
    [EnableClientAccess()]
    public class DomainServiceDB : LinqToEntitiesDomainService<ASPNETDBEntities>
    {

        // TODO:
        // Consider constraining the results of your query method.  If you need additional input you can
        // add parameters to this method or create additional query methods with different names.
        // To support paging you will need to add ordering to the 'aspnet_Roles' query.
        public IQueryable<aspnet_Roles> GetAspnet_Roles()
        {
            return this.ObjectContext.aspnet_Roles;
        }

        public void InsertAspnet_Roles(aspnet_Roles aspnet_Roles)
        {
            if ((aspnet_Roles.EntityState != EntityState.Detached))
            {
                this.ObjectContext.ObjectStateManager.ChangeObjectState(aspnet_Roles, EntityState.Added);
            }
            else
            {
                this.ObjectContext.aspnet_Roles.AddObject(aspnet_Roles);
            }
        }

        public void UpdateAspnet_Roles(aspnet_Roles currentaspnet_Roles)
        {
            this.ObjectContext.aspnet_Roles.AttachAsModified(currentaspnet_Roles, this.ChangeSet.GetOriginal(currentaspnet_Roles));
        }

        public void DeleteAspnet_Roles(aspnet_Roles aspnet_Roles)
        {
            if ((aspnet_Roles.EntityState == EntityState.Detached))
            {
                this.ObjectContext.aspnet_Roles.Attach(aspnet_Roles);
            }
            this.ObjectContext.aspnet_Roles.DeleteObject(aspnet_Roles);
        }

        // TODO:
        // Consider constraining the results of your query method.  If you need additional input you can
        // add parameters to this method or create additional query methods with different names.
        // To support paging you will need to add ordering to the 'Files' query.
        public IQueryable<File> GetFiles()
        {
            
            return this.ObjectContext.Files;
        }
        public File GetFileByID(int FileID)
        {

            return (from f in this.ObjectContext.Files.Include("aspnet_Roles") where (f.FileID == FileID) select f).First<File>(); 
        }

        public IQueryable<File> GetFilesInRole()
        {
            LinkedList<File> list = new LinkedList<File>(); 
            
            foreach (var item in this.ObjectContext.Files.Include("aspnet_Roles"))
            {
                if ( item.aspnet_Roles != null) 
                    if  (this.ServiceContext.User.IsInRole(item.aspnet_Roles.RoleName) )
                        list.AddLast(item);
            }
            return list.AsQueryable<File>(); 
        }
        public void InsertFile(File file)
        {
            if ((file.EntityState != EntityState.Detached))
            {
                this.ObjectContext.ObjectStateManager.ChangeObjectState(file, EntityState.Added);
            }
            else
            {
                this.ObjectContext.Files.AddObject(file);
            }
        }

        public void UpdateFile(File currentFile)
        {
            this.ObjectContext.Files.AttachAsModified(currentFile, this.ChangeSet.GetOriginal(currentFile));
        }

        public void DeleteFile(File file)
        {
            if ((file.EntityState == EntityState.Detached))
            {
                this.ObjectContext.Files.Attach(file);
            }
            this.ObjectContext.Files.DeleteObject(file);
        }
    }
}


